import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, AlertController, LoadingController } from 'ionic-angular';

import { MsgDetailPage } from './msg-detail/msg-detail'
import { MsgProvider } from '../../providers/service/msg'
import { JpushService } from '../../providers/jpushService'
import { PageModel } from '../../models/page'
import { Keyboard } from '@ionic-native/keyboard';
/**
 * Generated class for the MsgPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
    selector: 'page-msg',
    templateUrl: 'msg.html',
    providers: [Keyboard]
})
export class MsgPage {

    constructor(public navCtrl: NavController,
        public msgProvider: MsgProvider,
        public loadingCtrl: LoadingController,
        public alertCtrl: AlertController,
        public jpushService: JpushService,
        public keyboard: Keyboard,
        public navParams: NavParams) {
    }

    isReady = false;
    isLoading = true;
    pageObj = new PageModel();
    searchKey = '';
    list = []

    ionViewDidLoad() {

        this.jpushService.setBadge(0);
        this.isReady = true;
        this.getDataList(true);

    }

    /**
     * 下拉刷新
     * @param refresher
     */
    doRefresh(refresher) {
        this.getDataList(true).then(() => {
            refresher.complete();
        })
    }

    /**
     * 上拉加载
     * @param infiniteScroll
     */
    doInfinite(infiniteScroll) {
        this.getDataList().then(() => {
            infiniteScroll.complete();
        })
    }

    //搜索
    searchOnHandler() {
        this.getDataList(true);
    }

    //搜索取消
    searchOnCancel(event) {
        event.stopPropagation();
        this.searchOnHandler();
        setTimeout(function () {
            console.log(this.keyboard)
            try {
                this.keyboard || this.keyboard.close(); //关闭keyboard
            } catch (e) { }
            event.target.blur();
        }, 500);
    }

    getDataList(isRefresh = false) {
        this.isLoading = true;
        if (isRefresh) {
            this.pageObj.pageNum = 1;
            this.list = [];
        }

        let elseFilter = {
            title: this.searchKey
        }

        return this.msgProvider.getMsgList({ ...this.pageObj, ...elseFilter }).then((res: any) => {
            console.log(res);
            this.pageObj.pageSize = res.pageSize;
            this.pageObj.pageNum = res.pageNum;
            this.pageObj.total = res.total;
            this.pageObj.isLastPage = res.isLastPage;
            this.pageObj.pageNum++;
            this.isLoading = false;
            if (!res.list) {
                res.list = [];
            }
            this.list = this.list.concat(res.list);
        }, err => {
            this.isLoading = false;
        });

    }

    pToDetail(item) {

        //标记为已读
        if (!item.readFlag) {
            this.msgProvider.markReadMsg(item.id).then(res => {
                item.readFlag = true;
            });
        }
        this.navCtrl.push(MsgDetailPage, { id: item.id });
    }

    clearAll() {
        let confirm = this.alertCtrl.create({
            title: '提示',
            message: '确定清空所有消息?',
            buttons: [
                {
                    text: '取消',
                    handler: () => {
                        //console.log('Disagree clicked');
                    }
                },
                {
                    text: '确定',
                    handler: () => {

                        let loading = this.loadingCtrl.create({
                            content: '正在操作'
                        }); 
                        loading.present();
                        this.msgProvider.clearMsgAll().then(res=>{
                           
                            setTimeout(() => {
                                loading.dismiss();
                                this.getDataList(true);
                            }, 1000);
                            
                        },err=>{
                            loading.dismiss();
                        });
                        console.log('Agree clicked');
                    }
                }
            ]
        });
        confirm.present();
    }

}
